'Imports System.Net
Imports System.IO
Imports System.Text

Public Class Daz3DImporter

    Private Const HistoryUrl As String = "https://www.daz3d.com/i.x/account/orderitem_hist/-/?"

    Private Const PurchaseTableStart As String = "<TABLE class=TD60trans BORDER=0 WIDTH=100% CELLPADDING=5 CELLSPACING=0>"
    Private Const PurchaseTableEnd As String = "</TABLE>"

    Private Const RowStart As String = "<TR bgcolor=>"
    Private Const RowEnd As String = "</TR>"

    Private Const ColumnStart As String = "<TD>"
    Private Const ColumnEnd As String = "</TD>"

    Private Const OrderNumberStart As String = "/-/?order="
    Private Const OrderNumberEnd As String = "'>"

    Private Const ProductCodeColumnIndex As Integer = 3
    Private Const OrderDateColumnIndex As Integer = 4
    Private Const OrderIdColumnIndex As Integer = 6

    Private Const Empty As String = ""

    Private Function GetPageContents(ByVal filePath As String) As String

        'Dim request As HttpWebRequest = CType(WebRequest.Create(HistoryUrl), _
        '                                          HttpWebRequest)
        'request.KeepAlive = False

        'Dim response As HttpWebResponse = CType(request.GetResponse(), _
        '                                        HttpWebResponse)

        'Dim receiveStream As Stream = response.GetResponseStream()
        'Dim readStream As New StreamReader(receiveStream, _
        '                                   Encoding.UTF8)

        'Dim pageContents As String = readStream.ReadToEnd

        'readStream.Close()
        'response.Close()

        Dim sr As New StreamReader(filePath)
        Dim pageContents As String = sr.ReadToEnd

        Return pageContents

    End Function

    Private Function ParseRow(ByVal rowHtml As String) As PurchaseDetails

        Dim orderNumber As String = ""
        Dim orderDate As String = ""
        Dim productCode As String = ""

        Dim columnStartIndex, columnEndIndex As Integer
        Dim columnCounter As Integer = 0

        columnStartIndex = rowHtml.IndexOf(ColumnStart)

        While columnStartIndex > -1

            columnCounter += 1

            rowHtml = rowHtml.Substring(columnStartIndex)

            columnEndIndex = rowHtml.IndexOf(ColumnEnd)

            Select Case columnCounter
                Case ProductCodeColumnIndex
                    productCode = rowHtml.Substring(0, _
                                                    columnEndIndex)
                    productCode = productCode.Replace(ColumnStart, _
                                                      Empty)
                Case OrderDateColumnIndex
                    orderDate = rowHtml.Substring(0, _
                                                    columnEndIndex)
                    orderDate = orderDate.Replace(ColumnStart, _
                                                      Empty)
                Case OrderIdColumnIndex
                    orderNumber = rowHtml.Substring(0, _
                                                    columnEndIndex)
                    orderNumber = orderNumber.Replace(ColumnStart, _
                                                      Empty)
                    orderNumber = orderNumber.Substring(orderNumber.IndexOf(OrderNumberStart))
                    orderNumber = orderNumber.Substring(0, _
                                                        orderNumber.IndexOf(OrderNumberEnd))
                    orderNumber = orderNumber.Replace(OrderNumberStart, _
                                                      Empty)
            End Select


            rowHtml = rowHtml.Substring(columnEndIndex)

            columnStartIndex = rowHtml.IndexOf(ColumnStart)

        End While

        Return New PurchaseDetails(orderNumber, _
                                   orderDate, _
                                   productCode)

    End Function


    Private Function ParsePage(ByVal pageContents As String) As System.Collections.ObjectModel.Collection(Of PurchaseDetails)

        Dim purchases As New System.Collections.ObjectModel.Collection(Of PurchaseDetails)
        Dim rowHtml As String = ""

        Try

            'Dim pageContents As String = GetPageContents()

            Dim tableStartIndex As Integer = pageContents.IndexOf(PurchaseTableStart)
            Dim rowStartIndex As Integer = -1
            Dim rowEndIndex As Integer = -1

            If tableStartIndex > -1 Then

                pageContents = pageContents.Substring(tableStartIndex)
                pageContents = pageContents.Substring(0, _
                                                      pageContents.IndexOf(PurchaseTableEnd))

                rowStartIndex = pageContents.IndexOf(RowStart)

                While rowStartIndex > -1

                    pageContents = pageContents.Substring(rowStartIndex)
                    rowEndIndex = pageContents.IndexOf(RowEnd)

                    rowHtml = pageContents.Substring(0, _
                                                     rowEndIndex)

                    purchases.Add(ParseRow(rowHtml))

                    pageContents = pageContents.Substring(rowEndIndex)

                    rowStartIndex = pageContents.IndexOf(RowStart)

                End While

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Return purchases

    End Function

    Private Sub UpdateDatabase(ByVal purchases As System.Collections.ObjectModel.Collection(Of PurchaseDetails))

        Dim fa As New FileDataTableAdapters.FilesTableAdapter

        For Each purchase As PurchaseDetails In purchases

            Try

                If purchase.ProductCode.Trim.Length > 0 Then

                    fa.UpdateOrderDateAndNumber(CType(purchase.OrderDate, Date), _
                                                purchase.OrderId, _
                                                purchase.ProductCode, _
                                                ProviderNames.Daz3D)

                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        Next

    End Sub

    Public Sub Import(ByVal filePath As String)

        Dim pageContents As String = GetPageContents(filePath)
        Dim purchases As System.Collections.ObjectModel.Collection(Of PurchaseDetails) = ParsePage(pageContents)
        UpdateDatabase(purchases)

    End Sub

    Public Class PurchaseDetails

        Private _orderId As String = ""
        Private _orderDate As String = ""
        Private _productCode As String = ""

        Public ReadOnly Property OrderId() As String
            Get
                Return _orderId
            End Get
        End Property

        Public ReadOnly Property OrderDate() As String
            Get
                Return _orderDate
            End Get
        End Property

        Public ReadOnly Property ProductCode() As String
            Get
                Return _productCode
            End Get
        End Property

        Friend Sub New(ByVal orderId As String, _
                       ByVal orderDate As String, _
                       ByVal productCode As String)

            _orderId = orderId
            _orderDate = orderDate
            _productCode = productCode

        End Sub

    End Class

End Class
